home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
comm
/
cvt100.zip
/
COMIO.C
next >
Wrap
Text File
|
1988-07-31
|
14KB
|
337 lines
#include <stdio.h>
#include <dos.h>
#define MDMDAT1 0x03F8 /* Address of modem port 1 data */
#define MDMSTS1 0x03FD /* Address of modem port 1 status */
#define MDMCOM1 0x03FB /* Address of modem port 1 command */
#define MDMDAT2 0x02F8 /* Address of modem port 2 data */
#define MDMSTS2 0x02FD /* Address of modem port 2 status */
#define MDMCOM2 0x02FB /* Address of modem port 2 command */
#define MDMINTV 0x000C /* Com 1 interrupt vector */
#define MDINTV2 0x000B /* Com 2 interrupt vector */
#define MDMINTO 0x0EF /* Mask to enable IRQ3 for port 1 */
#define MDINTO2 0x0F7 /* Mask to enable IRQ4 for port 2 */
#define MDMINTC 0x010 /* Mask to Disable IRQ4 for port 1 */
#define MDINTC2 0x008 /* Mask to Disable IRQ3 for port 2 */
#define INTCONT 0x0021 /* 8259 interrupt controller ICW2-3 */
#define INTCON1 0x0020 /* Address of 8259 ICW1 */
#define COM_BUFF_SIZE 1024 /* Communications port buffer size */
#define XOFFPT COM_BUFF_SIZE*3/4 /* chars in buff before sending XOFF */
#define XONPT COM_BUFF_SIZE*1/4 /* chars in buff to send XON after XOFF */
#define XOFF 0x13 /* XOFF value */
#define XON 0x11 /* XON value */
/*****************************************************************************/
/* function prototypes */
void TTinit(); /* Initialize the communications system */
int ttopen(); /* Open a port for communications */
int ttclose( void ); /* Close the communications port */
int ttchk( void ); /* Return count of received characters */
void ttoc( unsigned char ); /* Output a character to the com port */
int ttinc( void ); /* Input a character from circular buffer */
void ttflui( void ); /* Flush circular buffer of characters */
int dobaud( unsigned int ); /* Set the baud rate for the port */
void coms( int ); /* Establish modem data */
void serini( void ); /* Initialize the com port for interrupts */
void serrst( void ); /* Reset the com port to original settings */
void interrupt serint( void ); /* Com port receiver ISR */
/*****************************************************************************/
/* Global Data */
unsigned int port; /* COM port */
unsigned int speed; /* BAUD rate */
char parity[5]; /* Parity setting */
unsigned int databits; /* Number of Data bits */
unsigned int stopbits; /* Number of Stop bits */
/*****************************************************************************/
/* External variables */
/*****************************************************************************/
/* Local Static Data */
static char buffer[COM_BUFF_SIZE];/* Circular buffer */
static char *inptr; /* Pointer to input point of circular buff*/
static char *outptr; /* Pointer to output point of circular buff*/
static int count = 0; /* Number of characters in buffer */
struct mdminfo { /* struct to hold current com port info */
unsigned int mddat; /* 8250 data register */
unsigned int mdstat; /* 8250 line-status register */
unsigned int mdcom; /* 8250 line-control register */
unsigned char mden; /* 8259 IRQ enable mask */
unsigned char mddis; /* 8259 IRQ disable mask */
unsigned char mdintv; /* Interrupt for selected com port */
} modem ;
void interrupt (*oldvec)(); /* Vector of previous com interrupt */
int portin = 0; /* Flag to indicate com port is open */
int xofsnt = 0; /* Flag to indicate an XOFF transmitted */
int xofrcv = 0; /* Flag to indicate an XOFF received */
/*****************************************************************************/
/* T T I N I T -- Initialize the communications system */
void TTinit() {
if (GetTTSetup() == 0) { /* If no saved values are available */
port = 1; /* Then set default values */
speed = 2400;
strcpy(parity,"NONE");
databits = 8;
stopbits = 1;
}
}
/* T T O P E N -- Open the communications port */
ttopen() {
if (portin == 0) { /* Ignore call if already open */
switch (port) {
case 1:
coms(1); /* COM 1 */
break;
case 2:
coms(2); /* COM 2 */
break;
default: /* others not supported, return error */
return(-1);
}
dobaud(speed); /* Set baud rate */
serini(); /* enable interrupt handler */
}
return(0); /* return success */
}
/* T T C L O S E -- Close the communications port */
ttclose() {
if (portin != 0) /* Ignore if port is already closed */
serrst(); /* otherwise disable interrupts */
return(0); /* return success */
}
/* T T C H K -- Return a count of characters at the serial port */
ttchk() {
return( count ); /* return maintained count */
}
/* T T O C -- Output a character to the current serial port */
void ttoc( unsigned char c ) {
while( (inportb(modem.mdstat) & 0x20) == 0 )
; /* Wait til transmitter is ready */
outportb(modem.mddat,c); /* then output the character */
}
/* T T F L U I -- Clear the input buffer of characters */
void ttflui() {
if (xofsnt){ /* Check if XON should be sent after XOFF */
xofsnt = 0; /* if so then reset XOFF sent status */
ttoc(XON); /* and send the XON */
}
disable(); /* NO interrupts allowed now */
inptr = outptr = buffer; /* Reset input out output pointers */
count = 0; /* Set received characters count to 0 */
enable(); /* Now interrupts are ok */
}
/* T T I N C -- Read a character from serial ports circular buffer */
ttinc() {
int c;
register char * ptr;
if (count < XONPT && xofsnt){ /* Check if XON should be sent after XOFF */
xofsnt = 0; /* if so then reset XOFF sent status */
ttoc(XON); /* and send the XON */
}
while (count == 0) /* If no characters have arrived then */
; /* wait til one arrives */
ptr = outptr; /* Save address of buffer output point */
c = *ptr++; /* Get this character and increment ptr */
/* See if circular buff should be wrapped */
if (ptr == &buffer[COM_BUFF_SIZE])
ptr = buffer; /* if so then save new output point */
disable(); /* NO interrupts allowed now */
outptr = ptr; /* Save the address of output point */
count--; /* Decrement count of received characters */
enable(); /* Interrupts can continue now */
return(c); /* Return the received character */
}
/* D O B A U D -- Set the baud rate for the current port */
dobaud( unsigned int baudrate ) {
unsigned char portval;
unsigned char blo, bhi;
switch (baudrate) { /* Get 8250 baud rate divisor values */
case 50: bhi = 0x